package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class ClientActionLis implements ActionListener {
	public void actionPerformed(ActionEvent action) {
		//System.out.println(e.getActionCommand());
		
		if(action.getActionCommand().equals("connect")) {
			Scanner scanner;
			try {
				//scanner = new Scanner(new URL("http://www.minihq.de/index.htm").openStream());
				String address = StartClient.clientProperties.getProperty("address");
				StartClient.chatOutput.append("\n Connecting to "+address);
				StartClient.chatOutput.append("address: "+new URL(address).toString());
				scanner = new Scanner(new URL(address).openStream());
				
				
				String table = "<table border=\"1\"><th>IP</th><th>Name</th><th>Informationen</th>";
				
				while (scanner.hasNext()) {
					String row = "<tr>";
					
					row = row+"<td>"+scanner.next()+"</td>";  //IP
					row = row+"<td>"+scanner.next()+"</td>";  //Name
					row = row+"<td>"+scanner.next()+"</td>";  //Sonstiges
	
					//Neue Zeile zur Tabelle
					table = table+row+"</tr>";
			    }
			
				
			    scanner.close();
			    
			    //System.out.println(table);
			    StartClient.chatOutput.append(table+"</table>");
			    
			    //TEST
			    StartClient.chatOutput.append("Size: "+StartClient.cm.cons.size());

			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else if(action.getActionCommand().equals("exit")) {
			System.exit(0);
		}
	}

}
